package com.nova.edc.service.energy.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.nova.edc.model.system.*;
import com.nova.edc.model.util.CountModel;
import com.nova.edc.service.system.DeviceTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.nova.edc.dao.energy.BillStatisticsDao;
import com.nova.edc.model.energy.BillStatistics;
import com.nova.edc.service.energy.BillStatisticsService;
import com.nova.edc.service.system.ElecEnergyService;
import com.nova.edc.service.system.ElecPriceService;
import com.nova.frame.dao.parsing.SQLParser;
import com.nova.frame.utils.JdbcUtils;
import com.nova.module.sql.SqlBuffer;

public class BillStatisticsServiceImpl implements BillStatisticsService {

    @Autowired
    private BillStatisticsDao novaBillStatisticsDao;

    @Autowired
    private ElecPriceService novaElecPriceService;

    @Autowired
    private ElecEnergyService novaElecEnergyService;

    @Autowired
    private DeviceTypeService novaDeviceTypeServicel;

    @Override
    public List<BillStatistics> getStatisticsByDay(String startDate,
                                                   String endDate) {
        SqlBuffer sqlBuffer = new SqlBuffer(
                SQLParser.sql("billStatisticsSQL.getStatisticsByDay"), true);
        List<BillStatistics> result = JdbcUtils.query(BillStatistics.class,
                sqlBuffer.getSelectSql(), startDate, endDate);
        for (BillStatistics billStatistics : result) {
            ElecPrice elecPrice = novaElecPriceService
                    .findElecPriceByOneCompanyCode(billStatistics
                            .getCompanycode());
            if (elecPrice != null && elecPrice.getUnitprice() != null) {
                billStatistics.setTracost(billStatistics.getTrapower()
                        .multiply(elecPrice.getUnitprice()));
                billStatistics.setLedcost(billStatistics.getLedpower()
                        .multiply(elecPrice.getUnitprice()));
                billStatistics.setRealcost(billStatistics.getRealpower()
                        .multiply(elecPrice.getUnitprice()));
            } else {
                billStatistics.setTracost(BigDecimal.ZERO);
                billStatistics.setLedcost(BigDecimal.ZERO);
                billStatistics.setRealcost(BigDecimal.ZERO);
            }
        }
        return result;
    }

    @Override
    public void insert(List<BillStatistics> billStatistics) {
        novaBillStatisticsDao.insert(billStatistics);
    }

    @Override
    public Map<String, BigDecimal> getAllStatistics(List<Integer> companyCodes) {
        SqlBuffer sqlBuffer = new SqlBuffer(
                SQLParser.sql("billStatisticsSQL.getAllStatistics"), true);
        List<Map<String, Object>> result = JdbcUtils.queryMap(sqlBuffer
                .getSelectSql().replace("#",
                        StringUtils.join(companyCodes, ",")));
        BigDecimal savePower = (BigDecimal) result.get(0).get(
                ModelConstant.SAVE_POWER);
        savePower = savePower == null ? BigDecimal.ZERO : savePower;
        BigDecimal saveCost = (BigDecimal) result.get(0).get(
                ModelConstant.SAVE_COST);
        saveCost = saveCost == null ? BigDecimal.ZERO : saveCost;
        Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
        // 获取当天时间内节省的电量-----由于获取当天电量和金钱耗费大量时间，因此暂时先不获取
        // for (Integer companyCode : companyCodes) {
        // Map<String, BigDecimal> todayPower = novaEnergyService
        // .getDetailEnergyByCompany(companyCode,
        // DateTimeUtil.getNowTime(DateTimeUtil.YYYY_MM_DD)
        // + "00:00:00",
        // DateTimeUtil.getNowTime(DateTimeUtil.YYYY_MM_DD)
        // + "23:59:59");
        // BigDecimal save = todayPower.get(ModelConstant.TRA_POWER).subtract(
        // todayPower.get(ModelConstant.LED_POWER));
        // ElecPrice elecPrice = novaElecPriceService
        // .findElecPriceByOneCompanyCode(companyCode);
        // if (elecPrice != null) {
        // saveCost.add(save.multiply(elecPrice.getUnitprice()));
        // }
        // savePower.add(save);
        // }

        ElecEnergy elecEnergy = novaElecEnergyService
                .findByPowerCode(ModelConstant.SOCIAL_CO2);

        map.put(ModelConstant.SAVE_POWER, savePower);
        map.put(ModelConstant.SAVE_COST, saveCost);

        BigDecimal co2 = savePower.multiply(
                new BigDecimal(elecEnergy.getPowerstandardvalue())).divide(
                new BigDecimal(ModelConstant.SOCAIL_DIVIDE), 2);
        BigDecimal car = co2.divide(new BigDecimal(ModelConstant.SOCAIL_CAR),
                0, BigDecimal.ROUND_HALF_DOWN);
        map.put(ModelConstant.INDEX_CO2, co2);
        map.put(ModelConstant.INDEX_CAR, car);

        return map;
    }

    @Override
    public Map<String, BigDecimal> getUnitStatistics(String startDate,
                                                     String endDate) {
        SqlBuffer sqlBuffer = new SqlBuffer(
                SQLParser.sql("billStatisticsSQL.getUnitStatistics"), true);
        List<Map<String, Object>> result = JdbcUtils.queryMap(
                sqlBuffer.getSelectSql(), startDate, endDate);
        Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
        if (result.size() > 0) {
            Map<String, Object> resultMap = result.get(0);
            map.put(ModelConstant.UNIT_SAVE_POWER,
                    (BigDecimal) resultMap.get(ModelConstant.UNIT_SAVE_POWER));
            map.put(ModelConstant.UNIT_SAVE_COST,
                    (BigDecimal) resultMap.get(ModelConstant.UNIT_SAVE_COST));
        }
        return map;
    }

    @Override
    public Map<String, BigDecimal> getStatisticsByDate(Integer companycode,
                                                       String startDate, String endDate) {
        SqlBuffer sqlBuffer = new SqlBuffer(
                SQLParser.sql("billStatisticsSQL.getStatisticsByDate"), true);
        List<Map<String, Object>> result = JdbcUtils.queryMap(
                sqlBuffer.getSelectSql(), companycode, startDate, endDate);
        BigDecimal savePower = (BigDecimal) result.get(0).get(
                ModelConstant.SAVE_POWER);
        savePower = savePower == null ? BigDecimal.ZERO : savePower;
        BigDecimal saveCost = (BigDecimal) result.get(0).get(
                ModelConstant.SAVE_COST);
        saveCost = saveCost == null ? BigDecimal.ZERO : saveCost;
        Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();

        map.put(ModelConstant.SAVE_POWER, savePower);
        map.put(ModelConstant.SAVE_COST, saveCost);
        return map;

    }

    @Override
    public  List<Map<String, Object>> getStatisticsByDayCuscomer(CountModel countModel, List<CustomerInfo> customerInfoList) {

        if(customerInfoList!=null&&customerInfoList.size()>0){
            SqlBuffer sqlBuffer = new SqlBuffer(
                    SQLParser.sql("billStatisticsSQL.getStatisticsByDayCuscomer"), true);
            List<Integer> companyCodes = new ArrayList<Integer>();
            for (int i = 0; i < customerInfoList.size(); i++) {
                companyCodes.add(customerInfoList.get(i).getCompanycode());
            }
            DeviceType lightDevice = novaDeviceTypeServicel.getByName(ModelConstant.DEVICE_LIGHTENERGY);
            DeviceType airDevice = novaDeviceTypeServicel.getByName(ModelConstant.DEVICE_AIR);
            List<Map<String, Object>> maps = JdbcUtils.queryMap(sqlBuffer.getSelectSql().replace("#",
                    StringUtils.join(companyCodes, ",")),lightDevice.getId(), airDevice.getId(), countModel.getStartDate(), countModel.getEndDate());
            return maps;
        }else {
            return null;
        }

    }
}
